
import os
import subprocess
#csv_path:  [ [ displacement][elements_nodes]  [][] ...]
#r_: [ [1,2,3],... ]
#//是否自动计算角度,适用于边裂纹,//如果自动计算角度，那么起始点dir_point和角度范围interp_angle架构无效
def interp(csv_path,r_,circle_point_count=100,center=[0,0,0],direction=[1,0,0],result_dir="../interp_result/"):
    print("interp displacement")
    #exe路径
    disp_files=[]
    root=os.getcwd()
    print(root)
    bat_file_path="disp_interpretion"
    os.chdir("step2-interpolation-displacement")

    for i in range(len(csv_path)):
        disp=csv_path[i][0]
        elemnodes=csv_path[i][1]
        r=r_[i]

        file_name_with_extension = os.path.basename(disp)
        disp_name, file_extension = os.path.splitext(file_name_with_extension)

        print("process ",disp)
       
        cmd=[]
        cmd.append("elements_nodes="+elemnodes)
        cmd.append("displacement="+disp)
        cmd.append("center="+",".join([str(num) for num in center]))
        cmd.append("r="+",".join([str(num) for num in r]))
        cmd.append(f"result={result_dir}"+disp_name+"_"+".csv")
        cmd.append("direction="+",".join([str(num) for num in direction]))
        cmd.append("circle_point_count="+str(circle_point_count))
       
        # 指定要写入的文件名
        disp_files.append(disp_name+"_")

        with open("displacement.txt", 'w') as file:
            # 遍历列表中的每个元素
            for item in cmd:
                # 将元素写入文件，每个元素占一行
                file.write(item + '\n')  # '\n' 是换行符
        #执行
        try:
            result = subprocess.run(['cmd', '/c', bat_file_path], check=True, text=True, capture_output=True)
        except subprocess.CalledProcessError as e:
            raise  "插值出错，有可能插值点超出有限元网格范围"
            return [disp_files,False]

    os.chdir(root)
    return [disp_files,True]